/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.utils;

import java.net.MalformedURLException;
import java.net.URL;

import dk.redia.lib.config.RediaConfig;
import dk.redia.lib.log.Log;

public class ImageUrlUtils {
	public static final String RESIZE_MODE_CROP_EDGES = "crop";
    public static final String RESIZE_MODE_BOUNDING_BOX = "bb";
    public static final String OUTPUT_TYPE_JPEG = "jpeg";
    public static final String OUTPUT_TYPE_PNG = "png";
    
	private static final String logTag = "ImageUrlUtils";
    
    /**
     * Get a URL for a resized selection from a source image.
     * @param url URL to the source image. Jpeg, gif and png data is supported.
     * @param width The output image width
     * @param height The output image height
     * @param mode Constant definining how the source image should be resized. Supported modes are RESIZE_MODE_CROP_EDGES and RESIZE_MODE_BOUNDING_BOX
     * @param selectionX X coordinate of the upper left corner of the rectangular selection. Must be non-negative to enable selections.
     * @param selectionY Y coordinate of the upper left corner of the rectangular selection. Must be non-negative to enable selections.
     * @param selectionWidth The width of the rectangular selection. Must be positive to enable selections.
     * @param selectionHeight The height of the rectangular selection. Must be positive to enable selections.
     */
    public static String getResizedImageUrl(String url, int width, int height, String mode, int selectionX, int selectionY, int selectionWidth, int selectionHeight, String output) {
            return RediaConfig.getImagesWebServiceBaseUrl() + mode + 
            		"&q=2" +
            		"&u=" + new String(Base64.encode(url.getBytes(), Base64.DEFAULT | Base64.NO_WRAP)) + 
            		"&w=" + Integer.valueOf(width) + 
            		"&h=" + Integer.valueOf(height) + 
            		"&sx=" + Integer.valueOf(selectionX) + 
            		"&sy=" + Integer.valueOf(selectionY ) + 
            		"&sw=" + Integer.valueOf(selectionWidth) + 
            		"&sh=" + Integer.valueOf(selectionHeight) +
            		"&o=" + output + 
            		"&a=" + AppConfiguration.getInstance().getApiKey();
    }
    
    public static URL getResizedImageUrl(URL url, int width, int height, String mode) {
    	try {
    		return new URL(getResizedImageUrl(url.toExternalForm(), width, height, mode));
    	} catch (MalformedURLException e) {
    		e.printStackTrace();
    		throw new RuntimeException("Invalid URL generated in getResizedImageUrl. This should definitely never happen. But it just did. How about that ...");
    	}
    }
    
    public static URL getResizedImageUrl(URL url, int width, int height, String mode, String output) {
    	try {
    		return new URL(getResizedImageUrl(url.toExternalForm(), width, height, mode, output));
    	} catch (MalformedURLException e) {
    		e.printStackTrace();
    		throw new RuntimeException("Invalid URL generated in getResizedImageUrl. This should definitely never happen. But it just did. How about that ...");
    	}
    }
    
    public static String getResizedImageUrl(String url, int width, int height, String mode) {
    	String resizedUrl = ImageUrlUtils.getResizedImageUrl(url, width, height, mode, -1, -1, -1, -1, "jpeg");
    	Log.d(ImageUrlUtils.logTag, "Returning URL of length " + resizedUrl.length());
    	return resizedUrl;
    }
    
    public static String getResizedImageUrl(String url, int width, int height, String mode, String output) {
    	String resizedUrl = ImageUrlUtils.getResizedImageUrl(url, width, height, mode, -1, -1, -1, -1, output);
    	Log.d(ImageUrlUtils.logTag, "Returning URL of length " + resizedUrl.length());
    	return resizedUrl;
    }
}
